package com.example.administrator.tenyuanpackage.speacial.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewTreeObserver;

import com.example.administrator.tenyuanpackage.R;
import com.example.administrator.tenyuanpackage.speacial.adapter.TenRecyclerViewAdapter;
import com.example.administrator.tenyuanpackage.speacial.bean.TenBean;
import com.example.administrator.tenyuanpackage.speacial.util.IApi;
import com.example.administrator.tenyuanpackage.speacial.util.NetUtils;
import com.example.administrator.tenyuanpackage.speacial.util.URLConfigs;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;



public class TenFragment extends LazyFragment {

    private IApi mIApi = NetUtils.getInstance().getApi();
    private List<TenBean.RowsBean> list=new ArrayList<>();
    private TenRecyclerViewAdapter adapter;
    private RecyclerView recyclerView;
    private List<TenBean.RowsBean> rows;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    public static Fragment getInsatance_ten(int i) {
        TenFragment fragment = new TenFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("typ", i);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void init(View view) {

        final int typeIndex = getArguments().getInt("typ");
        final int shang[] = {3, 4, 5, 3};

        recyclerView = (RecyclerView) view.findViewById(R.id.ten_RecyclerView);
        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.ten_swipeRefreshLayout);

        //瀑布流效果
        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        adapter = new TenRecyclerViewAdapter(getContext(),list);
        recyclerView.setAdapter(adapter);

        //通过观察者自动加载
        root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mSwipeRefreshLayout.setRefreshing(true);
                toJSON(URLConfigs.ten_url+shang[typeIndex]+URLConfigs.ten_hou);

                //取消观察者
                root.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        });


    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_ten;
    }


    //网络请求
    public void toJSON(String path){
        Call<String> call = mIApi.getData(path);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                mSwipeRefreshLayout.setRefreshing(false);
                String json = response.body();


                for (int i = 0; i < JsonToData(json).getRows().size(); i++) {
                    list.add(JsonToData(json).getRows().get(i));
                }
                adapter.notifyDataSetChanged();

            }


            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }

    public TenBean JsonToData(String json){
        Gson gson=new Gson();
        return gson.fromJson(json,new TypeToken<TenBean>(){}.getType());
    }


}
